package
{
import com.waitingsnow.core.base.BaseSprite;
import com.waitingsnow.core.debug.LogOutput;
import com.waitingsnow.core.debug.Logger;
import com.waitingsnow.core.util.core.StringUtil;

import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.system.Security;

/**
 * 
 * @author waitingsnow
 * $Id: GetYoukuUrl.as 2 2012-05-17 10:10:03Z cgslendy@gmail.com $
 */
public class GetYoukuUrl extends BaseSprite
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>GetYoukuUrl</code> instance.
	 * 
	 */
	public function GetYoukuUrl()
	{
		super();
	}
	
	//==========================================================================
	//  Variables
	//==========================================================================
	private var sid:String;
	private var reqUrl:String = "http://www.flvcd.com/parse.php?flag=&format=&kw={0}";
	private var swfUrl:String = "http://v.youku.com/v_show/id_XMzgzNDU2NDM2.html";
	private var resultArr:Array = [];
	
	private var urlLoader:URLLoader;
	
	//==========================================================================
	//  Methods
	//==========================================================================
	override protected function initDisplay():void
	{
		super.initDisplay();
		Security.allowDomain("*");
		Security.allowInsecureDomain("*");
		if (CONFIG::debugging)
		{
			var output:LogOutput = new LogOutput();
			addChild(output);
			removeChild(output);
		}
		
		testYouku();
	}
	
	private function testYouku():void
	{
		urlLoader = new URLLoader();
		urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
		urlLoader.addEventListener(Event.COMPLETE, loaderCompleteHandler);
		var url:String = StringUtil.substitute(reqUrl, swfUrl);
		urlLoader.load(new URLRequest(url));
		Logger.debug("start request: " + url);
	}
	
	private function loaderCompleteHandler(event:Event):void
	{
		var str:String = urlLoader.data;
		var arr:Array = str.match(/http:\/\/f.youku.com.*" t/g);
		resultArr = (arr.toString().substr(0, -3)).split('" t,');
		for each (var item:String in resultArr)
		{
			Logger.debug("ss: " + item);
		}
		Logger.debug(arr.length);
		
		play(resultArr[0]);
	}
	
	private function play(soundStr:String):void
	{
		
	}
}
}